package com.googlecode.imanager.client.style;

import java.io.Serializable;

import com.googlecode.imanager.client.bridge.style.IBackground;
import com.googlecode.imanager.client.media.Image;
import com.googlecode.imanager.client.util.Utils;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class Background implements IBackground, Serializable {

    private static final long serialVersionUID = 1L;
    
    public static final Background EMPTY_BACKGROUND = new Background(
            Color.TRANSPARENT, Image.EMPTY_IMAGE, false, false, HorzAlignment.DEFAULT, VertAlignment.DEFAULT);

    private Color color;
    private Image image;
    private Boolean imageHorzRepeat;    
    private Boolean imageVertRepeat;
    private HorzAlignment imageHorzAlignment;
    private VertAlignment imageVertAlignment;

    public Background(Color color) {
        super();
        this.color = color;
    }

    public Background(Image image) {
        super();
        this.image = image;
    }

    public Background(Image image, Boolean imageHorzRepeat,
            Boolean imageVertRepeat) {
        super();
        this.image = image;
        this.imageHorzRepeat = imageHorzRepeat;
        this.imageVertRepeat = imageVertRepeat;
    }

    public Background(Image image, HorzAlignment imageHorzAlignment,
            VertAlignment imageVertAlignment) {
        super();
        this.image = image;
        this.imageHorzAlignment = imageHorzAlignment;
        this.imageVertAlignment = imageVertAlignment;
    }

    public Background(Color color, Image image, Boolean imageHorzRepeat,
            Boolean imageVertRepeat, HorzAlignment imageHorzAlignment,
            VertAlignment imageVertAlignment) {
        super();
        this.color = color;
        this.image = image;
        this.imageHorzRepeat = imageHorzRepeat;
        this.imageVertRepeat = imageVertRepeat;
        this.imageHorzAlignment = imageHorzAlignment;
        this.imageVertAlignment = imageVertAlignment;
    }

    public Color getColor() {
        return color;
    }

    public Image getImage() {
        return image;
    }

    public Boolean getImageHorzRepeat() {
        return imageHorzRepeat;
    }

    public Boolean getImageVertRepeat() {
        return imageVertRepeat;
    }

    public HorzAlignment getImageHorzAlignment() {
        return imageHorzAlignment;
    }

    public VertAlignment getImageVertAlignment() {
        return imageVertAlignment;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this){
            return true;
        }
        if (obj instanceof Background){
            Background b = (Background) obj;
            return Utils.isEquals(color, b.color)
                && Utils.isEquals(image, b.image)
                && Utils.isEquals(imageHorzRepeat, b.imageHorzRepeat)
                && Utils.isEquals(imageVertRepeat, b.imageVertRepeat)
                && Utils.isEquals(imageHorzAlignment, b.imageHorzAlignment)
                && Utils.isEquals(imageVertAlignment, b.imageVertAlignment);
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        
        if (color != null){
            hash = color.hashCode();
        }
        if (imageHorzRepeat != null){
            hash ^= imageHorzRepeat.hashCode();
        }
        if (imageVertRepeat != null){
            hash ^= imageVertRepeat.hashCode();
        }
        if (imageHorzAlignment != null){
            hash ^= imageHorzAlignment.hashCode();
        }
        if (imageVertAlignment != null){
            hash ^= imageVertAlignment.hashCode();
        }
        if (image != null){
            hash ^= image.hashCode();
        }
        
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder value = new StringBuilder();
        value.append("Background[");
        boolean next = false;
        
        if (color != null){
            next = true;
            value.append("color=").append(color);
        }
        if (imageHorzRepeat != null){
            if (next){
                value.append(", ");
            } else {
                next = true;
            }
            value.append("imageHorzRepeat=").append(imageHorzRepeat);
        }
        if (imageVertRepeat != null){
            if (next){
                value.append(", ");
            } else {
                next = true;
            }
            value.append("imageVertRepeat=").append(imageVertRepeat);
        }
        if (imageHorzAlignment != null){
            if (next){
                value.append(", ");
            } else {
                next = true;
            }
            value.append("imageHorzAlignment=").append(imageHorzAlignment);
        }
        if (imageVertAlignment != null){
            if (next){
                value.append(", ");
            } else {
                next = true;
            }
            value.append("imageVertAlignment=").append(imageVertAlignment);
        }
        if (image != null){
            if (next){
                value.append(", ");
            }
            value.append("image=").append(image);
        }
        
        value.append(']');
        return value.toString();
    }

}
